package com.huashun.ship.run.shiplock.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger2在线API文档生成工具配置类
 * @author yzh
 * @version 0.0.1
 * @since 20200510
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerApiConfig {
    @Value("${swagger.api.name}")
    private String apiName;
    @Value("${swagger.api.version}")
    private String apiVersion;
    @Value("${swagger.api.description}")
    private String apiDesc;
    @Value("${swagger.api.package-scan}")
    private String packageScan;
    @Value("${swagger.api.terms-of-service-url}")
    private String termsOfServiceUrl;
    @Value("${swagger.api.contact}")
    private String contract;

    @Bean
    public Docket createTransactionRestfulApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName(apiName)
                .select()
                .apis(basePackage(packageScan))
                .apis(new Swaggerfilter())
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(this.apiName + "在线文档")
                .description(this.apiDesc)
                .termsOfServiceUrl(termsOfServiceUrl)
                .contact(contract)
                .version(this.apiVersion)
                .build();
    }

    /**
     * 过滤页面路由
     */
    class Swaggerfilter implements Predicate<RequestHandler> {
        @Override
        public boolean apply(@Nullable RequestHandler requestHandler) {
            if(requestHandler.findAnnotation(ApiOperation.class).isPresent()){
                return true;
            }
            return false;
        }
    }

    //额外定义3个方法，让swagger可以支持为多个不同的包生成api文档
    public static Predicate<RequestHandler> basePackage(final String basePackage) {
        return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
    }

    private static Function<Class<?>, Boolean> handlerPackage(final String basePackage)     {
        return input -> {
            // 循环判断匹配
            for (String strPackage : basePackage.split("[,]")) {
                boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                if (isMatch) {
                    return true;
                }
            }
            return false;
        };
    }

    private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
        return Optional.fromNullable(input.declaringClass());
    }
}